#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define ull unsigned long long

const int N = 1e5 + 10;

int n;
int ans = 0;
int a[N], b[N];

struct BIT {
#define low(x) ((x)&(-x))
    int sum[N];
    int lim;
    void init(int lim_) {
        lim = lim_;
        for (int i = 1; i <= lim; ++i) sum[i] = 0;
    }
    void insert(int p_, int val_) {
        for (int i = p_; i <= lim; i += low(i)) {
            sum[i] += val_;
        }
    }
    int Sum(int p_) {
        int ret = 0;
        for (int i = p_; i; i -= low(i)) ret += sum[i];
        return ret;
    }
} tr[2];

signed main() {
    cin >> n;
    tr[0].init(n);
    tr[1].init(n);
    for (int i = 1;i <= n;++i) {
        int x;cin >> x;
        a[x] = i;
    }
    for (int i = 1;i <= n;++i) {
        int x;cin >> x;
        b[x] = i;
    }
    int r = 1;
    tr[0].insert(a[r], 1);
    tr[1].insert(b[r], 1);
    for (int l = 1;l <= n;++l) {
        while (r + 1 <= n && tr[0].Sum(a[r + 1]) == tr[1].Sum(b[r + 1])) {
            tr[0].insert(a[r + 1], 1);
            tr[1].insert(b[r + 1], 1);
            ++r;
        }
        ans += (r - l + 1);
        tr[0].insert(a[l], -1);
        tr[1].insert(b[l], -1);
    }
    cout << ans;
    return 0;
}
